<!-- 搜索书籍 -->
<script>
    search({
        tableElementId: "book-table",
        searchText: null,
        categoryId: null
    });

    function search({ tableElementId = "" }) {
        postRequest("/book/getFavoritesList", { token: localStorageUtils.getToken() })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    // console.log(data)

                    // 数据进行转换
                    var renderData = [];
                    data.forEach(element => {
                        var mainDivWidth = 96/*vw*/; // 定义div的宽度（用于计算表格中的数据的显示长度）
                        var columnWidth = [20, 15, 10, 35];
                        renderData.push({
                            编号: `${element.id}`,
                            书名: ` <a target="_blank" href="/book?id=${element.id}">
                                        <span class="overflow-omit" style="max-width: ${columnWidth[0] * mainDivWidth / 100}vw; max-height: 2em; margin: 0 auto;">
                                            ${element.bookName}
                                        </span>
                                    </a>`,
                            分类: ` <a target="_blank" href="/category?id=${element.category.id}">
                                        <span class="overflow-omit" style="max-width: ${columnWidth[1] * mainDivWidth / 100}vw; max-height: 2em; margin: 0 auto;">
                                            ${element.category.name}
                                        </span>
                                    </a>`,
                            作者: `${element.author}`,
                            语言: `<span class="overflow-omit" style="max-width: ${columnWidth[2] * mainDivWidth / 100}vw; max-height: 2em; margin: 0 auto;">
                                        ${element.language}
                                    </span>`,
                            收藏: `<span style="display: block; width: 80px; max-height: 2em; margin: 0 auto;">
                                        <a id="favorites_button_${element.id}" href="javascript:toggleFavorites(false, ${element.id});">取消收藏</a>
                                    </span>`,
                        })
                    });

                    if (renderData.length == 0) {
                        console.log("没有搜索到相关书籍");
                        renderTable({ data: `在您的收藏夹没有找到电子书噢，快去收藏一本吧`, tableId: tableElementId, renderTableHead: true });
                    } else {
                        renderTable({ data: renderData, tableId: tableElementId, renderTableHead: true });
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            }).finally(function () {
                // 渲染后重新获取一次字体
                if (typeof (fontmin) === "function") {
                    fontmin(getPageText());
                }
            });
    }

    // 正在请求标记
    var requestingFlag = false;
    // 添加收藏/取消收藏
    function toggleFavorites(toggleStatus, bookId) {
        if (requestingFlag) return;
        requestingFlag = true;
        $("#favorties-button").css("opacity", "0.3");
        $("#favorites_button_" + bookId).html($("#favorites_button_" + bookId).html() + "中...");
        $("#favorites_button_" + bookId).css("color", "grey");
        postRequest("/book/toggleFavorites", { token: localStorageUtils.getToken(), bookId: bookId, status: toggleStatus ? 1 : 0 })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data)
                    if (data == "success") {
                        if (toggleStatus) {
                            console.log("收藏成功");
                            $("#favorites_button_" + bookId).html("取消收藏");
                            $("#favorites_button_" + bookId).attr("href", "javascript:toggleFavorites(false, " + bookId + ");");
                        } else {
                            console.log("取消收藏成功");
                            $("#favorites_button_" + bookId).html("收藏");
                            $("#favorites_button_" + bookId).attr("href", "javascript:toggleFavorites(true, " + bookId + ");");
                        }
                        $("#favorites_button_" + bookId).css("color", "");
                    } else {
                        console.log("操作失败");
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            }).finally(function () {
                requestingFlag = false;
            });
    }
</script>